package com.baizhi.utils.hutool;

import cn.hutool.core.convert.Convert;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * 简化类型转换
 */
public class ConvertTest {

    @Test
    public void test1() throws Exception {
        // 1.转换为字符串
        int a = 1;
        System.out.println(Convert.toStr(a));

        long[] b = {1, 2, 3, 4, 5};
        String bStr = Convert.toStr(b);
        System.out.println(bStr);

        // 2.转换为指定类型数组
        String[] c = {"1", "2", "3", "4"};
        Integer[] cArray = Convert.toIntArray(c);
        System.out.println(Arrays.toString(cArray));

        long[] d = {1, 2, 3, 4, 5};
        Integer[] dArray = Convert.toIntArray(d);
        System.out.println(Arrays.toString(dArray));

        // 3.字符串转为日期
        String str = "2012-12-12";
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").parse(str));
        System.out.println(Convert.toDate(str));
        str = "2012/12/12";
        System.out.println(Convert.toDate(str));

        // 4.数组转集合
        String[] array = {"hello", "world", "xiaochen"};
        List<String> listA = Arrays.asList(array);
        System.out.println(listA);
        List<String> listB = Convert.toList(String.class, array);
        System.out.println(listB);
    }
}
